<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('akun_model','video_model','kategori_model','komentar_model'));    
		$this->load->library(array('form_validation','session','pagination'));    
		$this->load->database();    
		$this->load->helper(array('url','form','string')); 
		//if($this->session->userdata('isLogin') == FALSE) {redirect('index.php/home');}	
	}
	
	public function load_data()
	{
		$username = $this->session->userdata('username');
		$data['akun'] = $this->akun_model->get_data_akun($username);
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
	}
	
	public function cekLog()
	{
		if(!$this->session->userdata('isLogin')) { redirect('index.php/home');}
	}
	
	public function index()
	{
		$this->cekLog();
		$username = $this->session->userdata('username');
		$data['akun'] = $this->akun_model->get_data_akun($username);
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		$data['nakun'] = $this->akun_model->get_banyak_akun();
		$data['nreviewer'] = $this->akun_model->get_list_reviewer();
		$data['reviewer_baru'] = $this->akun_model->get_reviewer_baru();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$data['nvideo'] = $this->video_model->get_banyak_video();
		$data['komentar_baru'] = $this->komentar_model->get_banyak_komentar_baru();
		$data['nkomentar'] = $this->komentar_model->get_banyak_komentar();
		$this->load->view('halaman_admin', $data);
	}
	
	
	 public function example1() {
        $config = array();
        $config["base_url"] = base_url() . "index.php/admin/example1";
        $config["total_rows"] = $this->akun_model->get_banyak_akun();
        $config["per_page"] = 5;
        $config["uri_segment"] = 3;
		
		$config['full_tag_open'] = "<ul class='pagination'>";
		$config['full_tag_close'] ="</ul>";
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
		$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
		$config['next_tag_open'] = "<li>";
		$config['next_tagl_close'] = "</li>";
		$config['prev_tag_open'] = "<li>";
		$config['prev_tagl_close'] = "</li>";
		$config['first_tag_open'] = "<li>";
		$config['first_tagl_close'] = "</li>";
		$config['last_tag_open'] = "<li>";
		$config['last_tagl_close'] = "</li>";
        $this->pagination->initialize($config);
 
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["akun"] = $this->akun_model->fetch_akun($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
 
        $this->load->view("halaman_master_akun", $data);
    }
	
	public function list_akun()
	{	
		$config = array();
		$config["base_url"] = base_url() . "index.php/admin/list_akun";
		$config["per_page"] = 5;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = "<ul class='pagination'>";
		$config['full_tag_close'] ="</ul>";
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
		$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
		$config['next_tag_open'] = "<li>";
		$config['next_tagl_close'] = "</li>";
		$config['prev_tag_open'] = "<li>";
		$config['prev_tagl_close'] = "</li>";
		$config['first_tag_open'] = "<li>";
		$config['first_tagl_close'] = "</li>";
		$config['last_tag_open'] = "<li>";
		$config['last_tagl_close'] = "</li>";
		
		$peran = $this->session->userdata('peran');
		if($peran == 1){
			
			$config["total_rows"] = $this->akun_model->get_banyak_reviewer();
			$data["n"] = $this->akun_model->get_banyak_reviewer();
			$this->pagination->initialize($config);
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$data['akun'] = $this->akun_model->fetch_reviewer($config["per_page"], $page);
			$data["links"] = $this->pagination->create_links();
			
		} else if($peran == 2){

			$config["total_rows"] = $this->akun_model->get_banyak_kontributor();
			$data["n"] = $this->akun_model->get_banyak_kontributor();
			$this->pagination->initialize($config);
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$data['akun'] = $this->akun_model->fetch_kontributor($config["per_page"], $page);
			$data["links"] = $this->pagination->create_links();
		} else { 
			$config["total_rows"] = $this->akun_model->get_banyak_akun();
			$data["n"] = $this->akun_model->get_banyak_akun();
			$this->pagination->initialize($config);
			$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
			$data['akun'] = $this->akun_model->fetch_akun($config["per_page"], $page);
			$data["links"] = $this->pagination->create_links();
		}
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		//$data['videotidakaktif'] = $this->video_model->get_banyak_video_tidak_aktif();
		
		if($this->session->flashdata('validasi_akun_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Proses perubahan validitas pengguna berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_akun_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Proses perubahan validitas pengguna gagal.</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
		
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$this->load->view('halaman_master_akun', $data);
	}
	
	public function hapus_akun($id)
	{
		$this->akun_model->hapus_akun($id);
		$data['akun'] = $this->akun_model->get_list_user();
		$this->load->view('halaman_master_akun', $data);
	}
	
	public function cari_akun()
	{	
		if($this->session->flashdata('validasi_akun_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Perubahan validasi berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_akun_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Proses perubahan validasi gagal.</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
	
		$data["links"] = '';
		
		$str = $this->input->post('cari');
		$peran = $this->session->userdata('peran');
		if($peran == 0){
			$data['akun'] = $this->akun_model->cari_akun($str);
		} else {
			$data['akun'] = $this->akun_model->cari_akun_reviewer($str);
		}
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		//$data['videotidakaktif'] = $this->video_model->get_banyak_video_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$this->load->view('halaman_master_akun', $data);
	}
	
	public function cari_reviewer()
	{	
		if($this->session->flashdata('validasi_akun_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Perubahan validasi berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_akun_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Proses perubahan validasi gagal.</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
	
		$config = array();
		$config["base_url"] = base_url() . "index.php/admin/cari_reviewer";
		$config["per_page"] = 5;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = "<ul class='pagination'>";
		$config['full_tag_close'] ="</ul>";
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
		$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
		$config['next_tag_open'] = "<li>";
		$config['next_tagl_close'] = "</li>";
		$config['prev_tag_open'] = "<li>";
		$config['prev_tagl_close'] = "</li>";
		$config['first_tag_open'] = "<li>";
		$config['first_tagl_close'] = "</li>";
		$config['last_tag_open'] = "<li>";
		$config['last_tagl_close'] = "</li>";		
		$config["total_rows"] = $this->akun_model->get_banyak_reviewer();
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$data['akun'] = $this->akun_model->fetch_reviewer($config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
		
		//$data['akun'] = $this->akun_model->cari_reviewer();
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		//$data['videotidakaktif'] = $this->video_model->get_banyak_video_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$this->load->view('halaman_master_akun', $data);
	}
	
	public function cari_kontributor()
	{	
		if($this->session->flashdata('validasi_akun_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Perubahan validasi berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_akun_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Proses perubahan validasi gagal.</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
	
		$config = array();
		$config["base_url"] = base_url() . "index.php/admin/cari_kontributor";
		$config["per_page"] = 3;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = "<ul class='pagination'>";
		$config['full_tag_close'] ="</ul>";
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
		$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
		$config['next_tag_open'] = "<li>";
		$config['next_tagl_close'] = "</li>";
		$config['prev_tag_open'] = "<li>";
		$config['prev_tagl_close'] = "</li>";
		$config['first_tag_open'] = "<li>";
		$config['first_tagl_close'] = "</li>";
		$config['last_tag_open'] = "<li>";
		$config['last_tagl_close'] = "</li>";		
		$config["total_rows"] = $this->akun_model->get_banyak_kontributor();
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$data['akun'] = $this->akun_model->fetch_kontributor($config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
		
		//$data['akun'] = $this->akun_model->cari_kontributor();
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		//$data['videotidakaktif'] = $this->video_model->get_banyak_video_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$this->load->view('halaman_master_akun', $data);
	}
	
	
	public function cari_video()
	{	
		$id_video = '';
		$data['berhasil'] = '';
		$str = $this->input->post('cari');
		$data['video'] = $this->video_model->cari_video($str);
		foreach ($data['video'] as $row) {
			$id_video = $row->id_video;
		}
		$data['kategori'] = $this->kategori_model->get_all_nama_video_kategori($id_video);
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		//$data['videotidakaktif'] = $this->video_model->get_banyak_video_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		$this->load->view('halaman_master_video', $data);
	}
	
	public function validasi_akun($id_user, $kode_status)
	{	
		
		if($this->akun_model->validasi($id_user, $kode_status) == true){
			$this->kirim_validasi($id_user);
			$this->session->set_flashdata('validasi_akun_sukses', time());
			redirect('index.php/admin/list_akun');
			return true;
		} else	{
			$this->session->set_flashdata('validasi_akun_gagal', time());
			redirect('index.php/admin/list_akun');
			return false;
		}
	}
	
	public function kirim_validasi($id_user)
	{
		
		$user = $this->akun_model->get_data_user($id_user);
		$config = Array(
		  'protocol' => 'smtp',
		  'smtp_host' => 'ssl://smtp.googlemail.com',
		  'smtp_port' => 465,
		  'smtp_user' => 'culturevid@gmail.com', 
		  'smtp_pass' => 'indonesia@raya#1', 
		  'mailtype' => 'html',
		  'charset' => 'iso-8859-1',
		  'wordwrap' => TRUE
		);
		
		$username = $user['username'];
		$nama_lengkap = $user['nama_lengkap'];
		$email_user = $user['email_user'];
		$status_user = $user['status_user'];
		if($status_user == 2){ $pesan = 'valid! Silahkan mulai upload video budaya anda di http://www.culture-vid.co.id/. Terima Kasih. '; $ucapan = 'Selamat';} else {$pesan = 'tidak valid!'; $ucapan = 'Maaf';}
		$message = $ucapan.' akun dengan username '.$username.' dinyatakan '.$pesan;
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from('culturevid@gmail.com'); 
		$this->email->to($email_user);
		$this->email->subject('Pemberitahuan Validasi Pengguna Culture-vid');
		$this->email->message($message);
		if($this->email->send())
		{
			echo 'Email Verifikasi Berhasil Terkirim!';
		}
			else
		{
		 show_error($this->email->print_debugger());
		}
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
